#! /usr/bin/env python

# Copyright (c) 2011 - 2012 Tony Bai <bigwhite.cn@gmail.com>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import os
import re
from utils import options
from utils.errnos import Errors
from core.core import Core
from core.glo import Glo

def buildc_run(opts, args):
    result      = None

    if opts.config != None:
        Glo.BUILDC_CFG_PATH = os.path.expanduser(opts.config)

        suffix_name = os.path.basename(Glo.BUILDC_CFG_PATH)
        if str(suffix_name).endswith(".cfg"):
            suffix_name = suffix_name[:-4]
        if str(suffix_name).startswith("buildc_"):
            suffix_name = suffix_name[7:]

        if suffix_name != "":
            Glo.PACK_SUFFIX = "_" + suffix_name

    if len(args) >= 3:
        if re.match("[a-zA-Z_][a-zA-Z_0-9]*=[a-zA-Z_0-9]+", args[2]) == None:
            print "Error: command [%s] does not support arg [%s] [%s]" % (args[0], args[1], args[2])
            sys.exit(Errors.args_invalid)

        suffix_name = args[2][args[2].find("=")+1:]

        Glo.VAR_STR = args[2]
        Glo.PACK_SUFFIX = "_" + suffix_name

    if args[0] == 'init':
        result = Core.init()
        return result

    if args[0] == 'cache':
        if args[1] == 'init':
            result = Core.cache_init()
            if result:
                print "Buildc cache initialization OK!"
            return result

        if args[1] == 'upgrade':
            result = Core.cache_upgrade()
            if result:
                print "Buildc cache upgrade OK!"
            return True

        if args[1] == 'update':
            result = Core.cache_update(opts.cmode, opts.ignore_error)
            if result:
                print "Buildc cache update OK!"
            else:
                print "Buildc cache update Error!"
            return result

        if args[1] == 'remove':
            result = Core.cache_remove(opts.cmode)
            if result:
                print "Buildc cache remove OK!"
            return result

        print "Error: command [%s] does not support arg [%s]" % (args[0], args[1])
        sys.exit(Errors.args_invalid)

    if args[0] == 'config':
        if args[1] == 'init':
            result = Core.config_init()
            return result

        if args[1] == 'make':
            if opts.cmode == None:
                opts.cmode = Glo.BIT64
            result = Core.config_make(opts.cmode, opts.force_update)
            if result:
                print "Buildc config make OK!"
            else:
                print "Buildc config make Error!"
            return result

        print "Error: command [%s] does not support arg [%s]" % (args[0], args[1])
        sys.exit(Errors.args_invalid)

    if args[0] == 'pack':
        if args[1] == 'create':
            result = Core.pack_create(opts.project)
            if result:
                print "Setup project [" + opts.project + "] create OK!"
            return result

        if args[1] == 'build':
            if opts.cmode == None:
                opts.cmode = Glo.BIT64
            result = Core.pack_build(opts.cmode, opts.tag, opts.force_update)
            if result:
                print "Buildc pack build OK!"
            else:
                print "Buildc pack build Error!"
            return result

        if args[1] == 'clean':
            result = Core.pack_clean()
            if result:
                print 'Buildc pack clean OK!'
            return result

        if args[1] == 'upload':
            result = Core.pack_upload(opts)
            if result:
                print "Buildc pack upload OK!"
            return result

        if args[1] == 'source':
            if opts.cmode == None:
                opts.cmode = Glo.BIT64
            result = Core.pack_source(opts.cmode, opts.tag, opts.force_update, opts.component)
            if result:
                print "Buildc pack source OK!"
            else:
                print "Buildc pack source Error!"
            return result

        print "Error: command [%s] does not support arg [%s]" % (args[0], args[1])
        sys.exit(Errors.args_invalid)

if __name__ == '__main__':
    result = None

    opts, args = options.options_parse()
    options.options_check(opts, args)

    result = buildc_run(opts, args)
    if result:
        sys.exit(0)
    else:
        sys.exit(1)
